package com.dome.git;

//二分查找
public class ArryLook {
    public static void main(String[] args) {
        //定义一个数组
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        //设定你要查找的元素
        int number = 3;
        //设定一个索引的变量,建设为-1
        int index = -1;
        //创建一个循坏,设定二分查找的最小值和最大值,得出中间值,循坏条件必须满足min<=max
        int min = 0;
        int max = arr.length - 1;
        while (min <= max) {
            int mid = (min + max) / 2;
            //判断设定的要查找的元素如果大于中间值,就说明要查找的元素在左边
            if (arr[mid] > number) {
                max = mid - 1;
                //否则相反
            } else if (arr[mid] < number) {
                min = mid + 1;
            } else {
                index = mid;
                break;
            }
        }
        System.out.println(index);
    }
}

